var AUTONOMY_EXAMINATION= (function(){
    function loadData(){
        $.ajax({
			type: "POST",
			url: "invs_autonomy_examination_homePage.do/load",
			data: {offset : '0',limit:'10'},
			success: function(data) {
				WeKnow.closeLoading();
				if (!WeKnow.filterAjaxResult(data)) return false; // session过期check

				if (data.errorcode == 0) {
                    // console.log(data);
                    var overallList = data.overallList;//全面自查列表
                    var overllNoExamination = data.count - data.overllHaveExamination;//未自查数量
                    // var overllNoExamination = data.overllNoExamination;//未自查数量
                    var overllHaveExamination = data.overllHaveExamination;//已自查数量
                    var overllNoEnforcement = overllNoExamination - data.overllHaveEnforcement; //未执法数量
                    // var overllNoEnforcement = data.overllNoEnforcement; //未执法数量
                    var overllHaveEnforcement = data.overllHaveEnforcement;//已执法数量
                    var dailyList = data.dailyList;//日常自查数据列表
                    var dailyNoExamination = data.count - data.dailyHaveExamination;//未自查数量
                    // var dailyNoExamination = data.dailyNoExamination;//未自查数量
                    var dailyHaveExamination = data.dailyHaveExamination;//已自查数量
                    var dailyNoEnforcement = dailyNoExamination - data.dailyHaveEnforcement;//未执法数量
                    // var dailyNoEnforcement = data.dailyNoEnforcement;//未执法数量
                    var dailyHaveEnforcement = data.dailyHaveEnforcement;//已执法数量
                    var count = data.count;//应查机构数
                    showTotal(overllNoExamination,overllHaveExamination,overllNoEnforcement,overllHaveEnforcement,count,"leftAutonomyExaminationSystem","全面");
                    showTotal(dailyNoExamination,dailyHaveExamination,dailyNoEnforcement,dailyHaveEnforcement,count,"rightAutonomyExaminationSystem","日常");
                    showList(overallList,'realTimeListLeft');
                    showList(dailyList,'realTimeListAbove');
                    $("#count").html(count);
				} else if (data.errorcode == -1 ) {
					WeKnow.errorMsg( data.errMsg );
				} else {
					WeKnow.systemMsg('查询失败，请重新登录');
				}
			}
		});
        
    }

    function showTotal(noExamination,haveExamination,noEnforcement,haveEnforcement,count,id,name){
        var $id = $('#'+id);
        var html ='';
        //已自查
        html += '<div style="height: 0.3rem;"><span class="first-span">已'+name+'自查</span><span class="second-span">'+(haveExamination/count*100).toFixed(1)+'%</span><span class="third-span">'+haveExamination+'</span></div>';
        //未自查
        html += '<div style="height: 0.3rem;"><span class="first-span">未'+name+'自查</span><span class="second-span">'+(noExamination/count*100).toFixed(1)+'%</span><span class="third-span">'+noExamination+'</span></div>';
        //待执法
        html += '<div style="height: 0.3rem;"><span class="first-span">待执法</span><span class="second-span">'+(noEnforcement/noExamination*100).toFixed(1)+'%</span><span class="third-span">'+noEnforcement+'</span></div>';
        //已执法
        html += '<div style="height: 0.3rem;"><span class="first-span">已执法</span><span class="second-span">'+(haveEnforcement/noExamination*100).toFixed(1)+'%</span><span class="third-span">'+haveEnforcement+'</span></div>';
          
        $id.empty();
        $id.html(html);
    }

    function showList(list,id){
        var $id = $('#'+id);
        var html = '';
        list.forEach(function (e) {
            html += '<li class="listItem"><span class="contentFont" style="width: 3rem">'+e.institutionName+'</span><span class="contentFont">'+countdown(e.year,e.quarter,e.enforcementSt)+'</span></li>';
        });

        $id.empty();
        $id.html(html);
    }

    function countdown(year, quarter, enforcementSt) {
        if (year && quarter && enforcementSt == '0') {
            // var createDate = new Date(parseInt(date)); //此条记录创建时间
            // var createMonth = createDate.getMonth();
            // var createQuarter = Math.floor((createMonth % 3 == 0 ? (createMonth / 3) : (createMonth / 3 + 1)));//季度

            var currDate = new Date(); //当期时间
            var currMonth = currDate.getMonth(); //获取当前月份(0-11,0代表1月)
            var currQuarter = Math.floor((currMonth % 3 == 0 ? (currMonth / 3) : (currMonth / 3 + 1)));//当前季度

            //数据查询出来的季度是1开始， js计算的季度从0开始
            if (((year == currDate.getFullYear()) && (quarter) == currQuarter) //今年
                || (((year + 1) == currDate.getFullYear()) && quarter == 4) //去年第四季度
            ) {
                // if ((createDate.getFullYear() == currDate.getFullYear())) {//今年
                if (currMonth == 0 || currMonth == 3 || currMonth == 6 || currMonth == 9) {
                    var day = (7 - currDate.getDate()) < 0 ? 0 : (7 - currDate.getDate());
                    return "剩余"+day + "天";
                } else {
                    return "剩余0天"
                }
            } else {
                return "剩余0天";
            }
        }else if (year && enforcementSt == '0') {
            var currDate = new Date(); //当期时间
            var currMonth = currDate.getMonth(); //获取当前月份(0-11,0代表1月)
            var currQuarter = Math.floor((currMonth % 3 == 0 ? (currMonth / 3) : (currMonth / 3 + 1)));//当前季度

            if ((year + 1) == currDate.getFullYear()) {
                if (currMonth == 0) { //1月
                    var day = (7 - currDate.getDate()) < 0 ? 0 : (7 - currDate.getDate());
                    return "剩余"+day + "天";
                } else {
                    return "剩余0天"
                }
            } else {
                return "剩余0天";
            }
        } else {
            return "";
        }

    }

    return {
        loadData:loadData,
    }

})();



$(document).ready(function() {
    AUTONOMY_EXAMINATION.loadData();
    // monitorRoll('realTimeListLeft');
    //monitorRoll('realTimeListAbove');
    //monitorRoll('realTimeListBelow');
});

//实时监控列表滚动
function  monitorRoll(id) {
    var $parent = $('#'+id+'');
    var $first = $parent.find('li:first');
    var height = $first.height();
    /*$first.animate({
        height: 0   //或者改成： marginTop: -height + 'px'
    }, 500, function() {// 动画结束后，把它插到最后，形成无缝
        $first.css('height', height).appendTo($parent);
    });*/
    $first.animate({
        height: 0
    }, 500, function () {
        $first.css('height', height).appendTo($parent);
    });
};